package com.guanzi.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guanzi.server.entity.Store;
import com.guanzi.server.entity.dto.StoreSaveDTO;
import com.guanzi.server.mapper.StoreMapper;
import com.guanzi.server.query.StoreListQuery;
import com.guanzi.server.query.StoreOrderListQuery;
import com.guanzi.server.service.IStoreService;
import com.guanzi.server.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guanzi
 * @since 2021-06-04
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Override
    public Pager<Store> getListByName(StoreListQuery storeListQuery) {
//        if ( limit <= 0 || limit == null ) {
//            return null;
//        }
//        QueryWrapper<Store> wrapper = new QueryWrapper<Store>();
//        wrapper.eq("is_del",0);
//        if ( name != null && name != "" ) {
//            wrapper.like("name",name);
//        }
//        IPage<Store> pageUtil = new Page<>(page,limit);
//        IPage<Store> p = storeMapper.selectPage(pageUtil, wrapper);
//        System.out.println("Current: "+ p.getCurrent() + ", count: " + p.getRecords().size() + ", size: " + p.getSize() + ", records: " + p.getRecords());
//        //（分页展示）逻辑处理,这里处理返回的记录显示
//        int index1 = ( page - 1 ) * limit;
//        int index2 = page * limit;
//        List<Store> stores = null;
//        if ( index1 < 0 ) {
//            stores = p.getRecords().subList(0, p.getRecords().size());
//        } else if ( index2 > p.getRecords().size() ) {
//            stores = p.getRecords().subList(index1, p.getRecords().size());
//        } else {
//            stores = p.getRecords().subList(index1, index2);
//        }
//        Pager<Store> pager = new Pager<>((long)p.getRecords().size(), stores );
//        return pager;


        //简单的方法
        QueryWrapper<Store> wrapper = new QueryWrapper<Store>();
        wrapper.eq("is_del",0);
        if ( storeListQuery.getName() != null && storeListQuery.getName() != "" ) {
            wrapper.like("name",storeListQuery.getName());
        }
        IPage<Store> pageUtil = new Page<>(storeListQuery.getPage(), storeListQuery.getLimit());
        IPage<Store> p = storeMapper.selectPage(pageUtil, wrapper);
        return new Pager<>((long)p.getRecords().size(), p.getRecords());



    }

    /**
     * 无条件时，查询所有
     * @return
     */
    @Override
    public Pager<Store> getList() {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<Store>().eq("is_del",0));
        List<Store> list = this.baseMapper.selectList(new QueryWrapper<Store>().eq("is_del",0));
        return  new Pager<Store>((long) count, list);
    }

    /**
     * 单个删除的方法
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //这里是逻辑删除，将is_del改为1
        Store store = baseMapper.selectById(id);
        store.setIsDel(1);
        baseMapper.updateById(store);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void delete(String... ids) {
        //批量更新is_del=1
        int[] storeIds = Arrays.stream(ids).mapToInt(Integer::parseInt).toArray();
        for (int storeId : storeIds) {
            //通过id查 => is_del=1 => 更新
            Store store = baseMapper.selectById(storeId);
            store.setIsDel(1);
            baseMapper.updateById(store);
        }
    }

    /**
     * 点餐列表展示
     * @param storeOrderListQuery
     * @return
     */
    @Override
    public List<Store> getOrderList(StoreOrderListQuery storeOrderListQuery) {
        if (storeOrderListQuery.getName() != null) {
            return baseMapper.selectList(new QueryWrapper<Store>().like("name",storeOrderListQuery.getName()).eq("is_del",0));
        }
        return baseMapper.selectList(new QueryWrapper<Store>().eq("is_del",0));
    }

    /**
     * 商家信息保存（含图片）
     * @param storeSaveDTO
     * @return
     */
    @Override
    public Boolean saveSTore(StoreSaveDTO storeSaveDTO) {
        //判断是否有id
            //有就是更新操作
            //无就是增加操作
        if ( storeSaveDTO.getId() != null ) {
            //更新
            Store store = storeMapper.selectById( storeSaveDTO.getId() );
            // 查询名称是否被使用
            Store temp = baseMapper.selectOne(new QueryWrapper<Store>().eq("name", store.getName()));
            if ( temp != null && !(temp.getId().equals(store.getId())) ) {
                return false;
            }
            store.setName( storeSaveDTO.getName() );
            store.setImg( storeSaveDTO.getImg() );
            store.setRemark( storeSaveDTO.getRemark() );
            baseMapper.updateById(store);
            return true;
        } else {
            //增加
            Store store = new Store();
            store.setName( storeSaveDTO.getName() );
            store.setImg( storeSaveDTO.getImg() );
            store.setRemark( storeSaveDTO.getRemark() );

            baseMapper.insert(store);
            return true;
        }
    }


}
